﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxTreeList;
using System.Data.SqlClient;
using System.Data;

public partial class GoodsInfo_GoodsManager : Frame.Pages.ActionInfomationEditBasePage
{
    protected override string ActionID
    {
        get { return "3001"; }
    }

    protected override string UserFunctions
    {
        get
        {
            if (CMSUser.Current.IsLogin)
            {
                if (CMSUser.Current.IsAdmin)
                {
                    return "IUDQ";
                }
                else
                {
                    return CMSUser.GetFunction(ActionID) != null ? CMSUser.GetFunction(ActionID).op : "";
                }
            }
            else
            {
                return "";
            }
        }
    }

    protected override bool IsShowSpecialFunctions
    {
        get
        {
            if (CMSUser.Current.IsLogin)
            {
                if (CMSUser.Current.IsAdmin)
                {
                    return true;
                }
                else
                {
                    return CMSUser.GetFunction(ActionID).op_s == "Y" ? true : false;
                }
            }
            else
            {
                return false;
            }
        }
    }

    protected override void Page_Load(object sender, EventArgs e)
    {
        gridView.SettingsText.PopupEditFormCaption = "编辑";
        deShow();
        base.Page_Load(sender, e);
    }
    protected void LSMDSourceMain_Inserting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceEditEventArgs e)
    {
        e.Values["founder"] = CMSUser.Current.ID;
        e.Values["fdate"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
        e.Values["editor"] = CMSUser.Current.ID;
        e.Values["edate"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
        e.Values["spxz"] = "Z";
    }
    protected void LSMDSourceMain_Updating(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceEditEventArgs e)
    {
        e.Values["editor"] = CMSUser.Current.ID;
        e.Values["edate"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
    }
    protected void gridView_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e)
    {
        if (e.Column.FieldName == "spbm")
        {
            if (e.Value == null)
            {
                using (DAL.DataClassesDataContext db = new DataClassesDataContext())
                {
                    int number = db.css_spjbxx.Count() + 1;
                    (e.Editor as ASPxTextBox).Text = WebUtility.GenerateCode(number.ToString(), 'S', 10);
                }
            }
        }
        if (e.Column.FieldName == "spmc")
        {
            e.Editor.Focus();
            (e.Editor as ASPxTextBox).ClientSideEvents.LostFocus = @"function(s,e){Edit_zjm.SetText(makePy(Edit_spmc.GetText()));Edit_spjc.SetText(Edit_spmc.GetText())}";
        }
        // e.Editor.SetClientSideEventHandler("KeyPress", @"function(s,e){var key = event.keyCode;if( key==13){EnterAsTab();}}");
    }
    //显示所有部门
    void deShow()
    {
        DataTable dt = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable("select * from css_department");
        this.DxTreelist.DataSource = dt;
        this.DxTreelist.DataBind();
    }
    //根据spbm获取当前商品所属部门
    void deShowById()
    {
        string bmbm;
        string id = txtSpbm.Text;
        string strSql = "select bmbm from css_spjbxx where spbm= '" + id + "'";
        bmbm = Frame.Global.GlobalSqlHelper.ExcuteSQLScalar(strSql).ToString();
        //清除所有选择    
        for (int i = 0; i < this.DxTreelist.Nodes.Count; i++)
        {
            this.DxTreelist.Nodes[i].Selected = false;
        }
        //显示当前商品所属部门
        TreeListNode selection = DxTreelist.FindNodeByKeyValue(bmbm);
        if (selection != null)
        {
            selection.Selected = true;
        }
    }

    //实现修改并保存当前商品的部门信息
    protected void QueryCBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        string bmbm = "";
        if (this.DxTreelist.GetSelectedNodes().Count > 0)   //判断treelist是否勾选了部门信息
        {
            List<TreeListNode> selection = this.DxTreelist.GetSelectedNodes();   //获取选定的部门信息
            //遍历node并连接成字符串
            foreach (TreeListNode node in selection)
            {
                bmbm = node.GetValue("bmbm").ToString();
            }
        }

        //实现修改指定商品的部门信息并保存
        List<SqlParameter> Params = new List<SqlParameter>();
        Params.Add(new SqlParameter("@spbm", txtSpbm.Text.ToString()));
        Params.Add(new SqlParameter("@bmbm", bmbm));
        Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("update css_spjbxx set bmbm=@bmbm where spbm=@spbm", Params.ToArray());
    }

    //当前生效促销编号详细信息
    protected void ContractPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        if (!string.IsNullOrEmpty(e.Parameter))
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                DataTable dt = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable("select * from css_sp_mjms where cxbh='" + e.Parameter + "'");
                this.cxInfo.DataSource = dt;
                this.cxInfo.DataBind();
            }
        }
    }
}